//
//  KTPayUtils.m
//  BoWuGuan
//
//  Created by 梁尚嘉 on 2016/12/1.
//  Copyright © 2016年 KT. All rights reserved.
//

#import "KTPayUtils.h"
#import "WXApi.h"
#import <AlipaySDK/AlipaySDK.h>
#import <CommonCrypto/CommonDigest.h>
#import "MainConfigure.h"
#import "DataMD5.h"
//#import "OMOrder.h"
//#import "DataSigner.h"

#import "OMOrder.h"
@implementation KTPayUtils

+ (void)WXPayWithOrder:(id)order
       paymentPluginId:(NSString *)paymentPluginId
          onCompletion:( void (^)(BOOL success))complection
{

    [self requestOrderPay:order
                   PayWay:KTPayWithWechat
          paymentPluginId:paymentPluginId
             OnCompletion:^(id responseObject, BOOL isSuccess, NSString *message) {

                 if (isSuccess) {
                     NSDictionary *responseDic = responseObject[OMDatumKey];
                     PayReq* req = [[PayReq alloc] init];
                     req.partnerId           = responseDic[@"partnerid"];
                     req.prepayId            = responseDic[@"prepayid"];
                     req.nonceStr            = responseDic[@"noncestr"];
                     req.timeStamp           = [responseDic[@"timestamp"] intValue];
                     req.package             = responseDic[@"package"];
                     req.openID              = responseDic[@"appid"];
                     req.sign                = responseDic[@"sign"];

                     BOOL success = [WXApi sendReq:req];
                     if (complection) {
                         complection(success);
                     }
                 }else {
                     if (complection) {
                         complection(NO);
                     }
                 }

             }];

}


+ (void)requestOrderPay:(OMOrder *)order
                 PayWay:(KTPayWay)payWay
        paymentPluginId:(NSString *)paymentPluginId
           OnCompletion:(KTHttpUtilCompletion)completion
{
    [KTHttpUtils POST:@"member/payment/submit.jhtml"
           parameters:@{@"sn":order.sn,@"paymentPluginId":paymentPluginId,@"type":@"payment"}
             progress:nil
           completion:completion];
}


+ (void)AlipayWithOrder:(OMOrder *)requestOrder
        paymentPluginId:(NSString *)paymentPluginId
           onCompletion:( void (^)(BOOL success))complection
{

    [self requestOrderPay:requestOrder
                   PayWay:KTPayWithAliPay
          paymentPluginId:paymentPluginId
             OnCompletion:^(id responseObject, BOOL isSuccess, NSString *message) {

                 if (isSuccess) {
                     //应用注册scheme,在Info.plist定义URL types
                     NSString *appScheme = @"omengo";

                     // NOTE: 调用支付结果开始支付
                     [[AlipaySDK defaultService] payOrder:responseObject[OMDatumKey] fromScheme:appScheme callback:^(NSDictionary *resultDic) {
                         NSLog(@"reslut = %@",resultDic);
                         if ([resultDic[@"resultStatus"] integerValue] == 9000) {
                             complection(YES);
                         }else {
                             complection(NO);
                         }
                     }];

                 }else {
                     if (complection) {
                         complection(NO);
                     }
                 }
                 [OMHUD HUDHide];
             }];
}



@end
